<html>
<head><meta charset="utf-8"><title>Compile time check in const generics · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Compile.20time.20check.20in.20const.20generics.html">Compile time check in const generics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239141662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Compile%20time%20check%20in%20const%20generics/near/239141662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> wata <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Compile.20time.20check.20in.20const.20generics.html#239141662">(May 17 2021 at 18:55)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">([</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">get</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">I</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="n">array</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Array</span>::<span class="o">&lt;</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="o">&gt;</span><span class="p">([</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">3</span><span class="p">]);</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">array</span><span class="p">.</span><span class="n">get</span>::<span class="o">&lt;</span><span class="mi">3</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The issue with this code as it is written, is that the former case will fail at compile with a nice and detailed error:</p>
<div class="codehilite"><pre><span></span><code>   Compiling playground v0.0.1 (/playground)
error: this operation will panic at runtime
  --&gt; src/main.rs:11:5
   |
11 |     array[3] = 4;
   |     ^^^^^^^^ index out of bounds: the length is 3 but the index is 3
   |
   = note: `#[deny(unconditional_panic)]` on by default

error: aborting due to previous error
</code></pre></div>
<p>while the latter case simply will run and panic at runtime, but compiler should have enough information to check for that. Is there any workaround for now till const generics get fully fleshed out?</p>
<p>Playground: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b01f09967e3161ec84bb1c139b65ee2d">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b01f09967e3161ec84bb1c139b65ee2d</a></p>



<a name="239143131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Compile%20time%20check%20in%20const%20generics/near/239143131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Compile.20time.20check.20in.20const.20generics.html#239143131">(May 17 2021 at 19:04)</a>:</h4>
<p>There are various static_/const_assert hacks that might help. But they lead to ugly error messages.</p>



<a name="239143293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Compile%20time%20check%20in%20const%20generics/near/239143293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> wata <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Compile.20time.20check.20in.20const.20generics.html#239143293">(May 17 2021 at 19:05)</a>:</h4>
<p>I see, I'll just wait till this feature is complete then, thanks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>